#!/bin/bash

# Slackware build script for unvanquished

# Copyright 2014-2016  Dimitris Zlatanidis  Orestiada, Greece
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20210216 bkw: modified by SlackBuilds.org, BUILD=2:
# - add -fpermissive to get it to compile
# - include the complete game data (*.pk3 files) so the game actually runs
# - add sysctl stuff to let the game actually start up
# Apparently this build never worked and nobody noticed.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=unvanquished
VERSION=${VERSION:-0.50.0}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCNAM=Unvanquished

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

EXT_DEPS="linux32-4"
if [ "$ARCH" = "x86_64" ]; then
    EXT_DEPS="linux64-4"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz || tar xvf $CWD/v$VERSION.tar.gz
cd $SRCNAM-$VERSION
cd daemon/external_deps
tar xvf $CWD/$EXT_DEPS.tar.bz2
cd -
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

if [ "$ARCH" = "x86_64" ]; then
   sed -i -e 's/Unvanquished/unvanquished/g' -e \
             's/\/lib/\/lib64/g' -e 's/share\//share\/games\//g' archlinux/unvanquished.sh
   sed -i -e 's/\/lib/\/lib64/g' -e 's/share\//share\/games\//g' archlinux/unvanquished-tty.sh
   sed -i -e 's/\/usr\/lib/\/usr\/lib64/g' -e \
             's/share\//share\/games\//g' archlinux/unvanquished.conf
   sed -i -e 's/\/lib/\/lib64/g' -e 's/conf.d\///g' archlinux/unvanquished.service
else
   sed -i -e 's/Unvanquished/unvanquished/g' -e \
             's/share\//share\/games\//g' archlinux/unvanquished.sh
   sed -i 's/share\//share\/games\//g' archlinux/unvanquished-tty.sh
   sed -i 's/share\//share\/games\//g' archlinux/unvanquished.conf
   sed -i 's/conf.d\///g' archlinux/unvanquished.service
fi

# Fix avoid download automatically dependencies
patch -p1 < $CWD/cmakelists.patch

mkdir -p build
cd build
  cmake \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS -fpermissive" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DBUILD_CGAME=OFF \
    -DBUILD_SGAME=OFF \
    -DCMAKE_BUILD_TYPE=Release ..
make

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/bin \
         $PKG/etc/$PRGNAM/config \
         $PKG/usr/lib${LIBDIRSUFFIX}/$PRGNAM \
         $PKG/usr/lib${LIBDIRSUFFIX}/systemd/system \
         $PKG/usr/share/games/$PRGNAM/pkg \
         $PKG/var/lib/unvanquished-server/config

# Install contents
install -D -m0755 daemon $PKG/usr/lib${LIBDIRSUFFIX}/$PRGNAM
install -D -m0755 daemon-tty $PKG/usr/lib${LIBDIRSUFFIX}/$PRGNAM
install -D -m0755 daemonded $PKG/usr/lib${LIBDIRSUFFIX}/$PRGNAM
install -D -m0755 irt_core-x86*.nexe $PKG/usr/lib${LIBDIRSUFFIX}/$PRGNAM
install -D -m0755 nacl_helper_bootstrap $PKG/usr/lib${LIBDIRSUFFIX}/$PRGNAM
install -D -m0755 nacl_loader $PKG/usr/lib${LIBDIRSUFFIX}/$PRGNAM

cd ..
# Install server configurations
install -D -m0644 debian/configfiles/server.cfg $PKG/etc/$PRGNAM/config/server.cfg
install -D -m0644 debian/configfiles/maprotation.cfg $PKG/etc/$PRGNAM/config/maprotation.cfg

# Install starters
install -D -m0755 archlinux/$PRGNAM.sh $PKG/usr/bin/$PRGNAM
install -D -m0755 archlinux/$PRGNAM-tty.sh $PKG/usr/bin/$PRGNAM-tty
install -D -m0644 archlinux/$PRGNAM.conf $PKG/etc/$PRGNAM.conf.new
install -D -m0644 archlinux/$PRGNAM.service \
                  $PKG/usr/lib${LIBDIRSUFFIX}/systemd/system/$PRGNAM.service

# Setup server home directory
ln -sf $PKG/etc/$PRGNAM/config/server.cfg \
       $PKG/var/lib/$PRGNAM-server/config/server.cfg
ln -sf $PKG/etc/$PRGNAM/config/maprotation.cfg \
       $PKG/var/lib/$PRGNAM-server/config/maprotation.cfg

# Copy the game data
mkdir -p $PKG/usr/share/games/$PRGNAM/pkg
unzip $CWD/${PRGNAM}_$VERSION.zip '*/pkg/*'
cp -a ${PRGNAM}_$VERSION/pkg/* $PKG/usr/share/games/$PRGNAM/pkg

mkdir -p $PKG/usr/share/applications
install -D -m644 $CWD/$PRGNAM.desktop $PKG/usr/share/applications/$PRGNAM.desktop

mkdir -p $PKG/usr/share/pixmaps/
install -D -m644 debian/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a GPL.txt README.md COPYING.txt $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# 20210216 bkw: sysctl is needed per
# https://github.com/Unvanquished/Unvanquished/issues/944
# It actually makes the game start up.
mkdir -p $PKG/etc/sysctl.d
cat $CWD/90-mmap_min_addr.conf > $PKG/etc/sysctl.d/90-mmap_min_addr.conf

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
